package com.wmwj.hxlj.controller;

import com.wmwj.hxlj.entity.Article;
import com.wmwj.hxlj.entity.vo.ArticleVo;
import com.wmwj.hxlj.service.ArticleService;
import com.wmwj.hxlj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    // 发布文章
    @PostMapping("/publish")
    public Result publishArticle(@RequestBody Article article) {
        return articleService.publishArticle(article);
    }

    // 删除文章
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteArticle(@PathVariable Long id) {
        return articleService.deleteArticle(id);
    }

    // 更新文章
    @PutMapping("/update")
    public Result updateArticle( @RequestBody Article article) {
        return articleService.updateArticle(article);
    }

    // 根据文章id获取文章详情
    @GetMapping("/{id}")
    public Result getArticleById(@PathVariable Long id) {
        return articleService.getArticleById(id);
    }


    //获取用户自己的草稿文章
    @GetMapping("/getUserDraftArticles/{id}")
    public Result<List<ArticleVo>> getUserDraftArticles(@PathVariable Long id) {
        return articleService.getUserDraftArticles(id);
    }

    // 新增方法：获取所有文章
    @GetMapping("/getAllArticles")
    public Result<List<ArticleVo>> getAllArticles(@RequestParam Integer pageStart) {
        return articleService.getAllArticles(pageStart);
    }

    //用户获取自己的文章
    @GetMapping("/getUserArticles/{id}")
    public Result<List<ArticleVo>> getUserArticles(@PathVariable Long id,@RequestParam Integer pageStart) {
        return articleService.getUserArticles(id, pageStart);
    }

    //文章点赞
    @GetMapping("/like")
    public Result<Integer> likeArticle(@RequestParam Long articleId, @RequestParam Long userId) {
        return articleService.likeArticle(articleId, userId);
    }


}